package com.miplus.ccrm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.admin.controller.vo.*;
import com.miplus.ccrm.admin.pojo.SysNames;
import com.miplus.ccrm.admin.pojo.SysTags;
import com.miplus.ccrm.admin.service.SysNamesService;
import com.miplus.ccrm.admin.mapper.SysNamesMapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【sys_names】的数据库操作Service实现
* @createDate 2024-03-02 17:02:10
*/
@Service
public class SysNamesServiceImpl extends ServiceImpl<SysNamesMapper, SysNames>
    implements SysNamesService{

    @Override
    public List<NamesResVo> getNamesValues(String tid,String pid)
    {
        return getBaseMapper().getNamesValues(tid,pid);
    }

    @Override
    public List<NamesNumberResVo> getNamesNumberValues(String tid,String pid)
    {
        List<NamesNumberResVo> resList=new LinkedList<>();
        List<NamesResVo> ll=getBaseMapper().getNamesValues(tid,pid);
        for(NamesResVo p : ll){
            NamesNumberResVo resVo=new NamesNumberResVo();
            resVo.setValue(JPubUtils.StringToInt(p.getValue()));
            resVo.setLabel(p.getLabel());
            resList.add(resVo);
        }
        return resList;
    }

    @Override
    public IPage<NamesInfoResVo> namesList(NamesInfoReqVo paras)
    {
        return baseMapper.namesList(new Page<>(paras.getPageNum(), paras.getPageSize()),paras);
    }

    @Override
    public List<NamesResVo> namesTypeSelect()
    {
        return baseMapper.namesTypeSelect();
    }

    @Override
    public List<NamesTypeResVo> namesType()
    {
        return baseMapper.namesType();
    }

    @Override
    public CmdResVo updateNames(NamesEditReqVo paras){
        SysNames n=new SysNames();
        n.setId(paras.getId());
        n.setDsc(paras.getName());
        n.setTid(paras.getTid());
        n.setTdsc(paras.getTdsc());
        n.setAvailable(paras.getStatus());
        n.setParentid(paras.getPid());

        //TDSQL [rank]是关键字，这样更新报错
        //n.setRank(paras.getRank());

        int ret=baseMapper.update(n,new QueryWrapper<SysNames>()
                .eq("tid",paras.getTid())
                .eq("id",paras.getId())
        );

        //for tdsql DBBASE,2027-07-22
        ret=baseMapper.updateRank(
                paras.getTid(),paras.getId(),paras.getRank()
        );

        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo updateStatus(UpTiDStatusReqVo paras)
    {
        SysNames n=new SysNames();
        n.setTid(paras.getTid());
        n.setId(paras.getId());
        n.setAvailable(paras.getStatus());
        int ret=baseMapper.update(n,new QueryWrapper<SysNames>()
                .eq("tid",paras.getTid())
                .eq("id",paras.getId())
        );

        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo insertNames(NamesEditReqVo paras)
    {
        SysNames n=new SysNames();
        n.setId(paras.getId());
        n.setDsc(paras.getName());
        n.setTid(paras.getTid());
        n.setTdsc(paras.getTdsc());
        n.setAvailable(paras.getStatus());
        n.setParentid(paras.getPid());
        //n.setRank(paras.getRank());

        int ret=baseMapper.insert(n);

        //for tdsql DBBASE,2027-07-22
        ret=baseMapper.updateRank(
                paras.getTid(),paras.getId(),paras.getRank()
        );

        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo deleteNames(String tid,String id)
    {
        int ret=baseMapper.delete(new QueryWrapper<SysNames>()
                .eq("tid",tid)
                .eq("id",id)
        );
        return new CmdResVo(ret);
    }

    @Override
    public List<NamesTreeResVo> namesTidTree(String tid)
    {
        //TDSQL里面rank是关键字，必须要：表别名.rank，否则报错
        //List<SysNames> all=baseMapper.selectList(new QueryWrapper<SysNames>().eq("tid",tid));
        List<SysNames> all=baseMapper.namesSelect(tid);
        List<NamesTreeResVo> tree = new ArrayList<>();
        //获取到最外层的组信息
        List<SysNames> parent = all.stream().filter(p -> JPubUtils.IsEmpty(p.getParentid())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(parent)){
            for(SysNames g : parent) {
                NamesTreeResVo resVo=new NamesTreeResVo();
                resVo.setId(g.getId());
                resVo.setName(g.getDsc());
                tree.add(resVo);
                addChildren(resVo, all);
            }
        }
        return tree;
    }

    private void addChildren(NamesTreeResVo resVo, List<SysNames> all) {
        // 拿到所传部门的字部门列表
        List<SysNames> tempList = all.stream()
                .filter(p -> resVo.getId().equals(p.getParentid()))
                .collect(Collectors.toList());
        List<NamesTreeResVo> resVoList=new ArrayList<NamesTreeResVo>();
        for(SysNames l: tempList){
            NamesTreeResVo r=new NamesTreeResVo();
            r.setId(l.getId());
            r.setName(l.getDsc());
            resVoList.add(r);
        }
        resVo.setChildren(resVoList);
        tempList.forEach(g -> {
            NamesTreeResVo resVo1=new NamesTreeResVo();
            resVo1.setId(g.getId());
            resVo1.setName(g.getDsc());
            addChildren(resVo1, all);
        });
    }
}




